from Structure import Stack

# 自定义基数进行进制转换
"""
通过栈的先进后出FILO特性实现
"""
def baseConverter(decNumber, base):
    digits = "0123456789ABCDEF"
    remstack = Stack()

    while decNumber > 0:
        rem = decNumber % base # 求余
        remstack.push(rem)
        decNumber = decNumber // base # 取整除，向下取整

    newString = ""
    while not remstack.isEmpty():
        newString = newString + digits[remstack.pop()]
    
    return newString

"""
通过自带列表的先进后出FILO特性实现
"""
def base_convert(decnumber, base):
    digits = "0123456789ABCDEF" # 十六进制可能出现的符号
    remlist = []

    while decnumber > 0:
        rem = decnumber % base
        remlist.append(rem)
        decnumber = decnumber // base

    newstring = ""
    while len(remlist) > 0:
        newstring = newstring + digits[remlist.pop()]

    return newstring

decnumber = 233
binnumber = base_convert(decnumber, 2)
binnumber2 = baseConverter(decnumber, 2)
print(binnumber)
print(binnumber2)